/*
 * Copyright (C) 2011 Julia Mlinnik
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jmlinnik.android.mediascanner;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.widget.RemoteViews;

public class MediaScanWidgetProvider extends AppWidgetProvider {

    private static Context mContext;
    private ComponentName mWidget;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        mContext = context;

        Intent intent = new Intent(
                Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory()));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.mediascan_appwidget);
        views.setOnClickPendingIntent(R.id.root, pendingIntent);

        mWidget = new ComponentName(context, MediaScanWidgetProvider.class);
        appWidgetManager.updateAppWidget(mWidget, views);

    }

    private void runScan() {
        RemoteViews views = new RemoteViews(mContext.getPackageName(),
                R.layout.mediascan_appwidget);
        views.setViewVisibility(R.id.scan_progress, View.VISIBLE);
        views.setViewVisibility(R.id.scan_image, View.GONE);

        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(mContext);
        mWidget = new ComponentName(mContext, MediaScanWidgetProvider.class);
        appWidgetManager.updateAppWidget(mWidget, views);

    }

    private void stopScan() {
        RemoteViews views = new RemoteViews(mContext.getPackageName(),
                R.layout.mediascan_appwidget);
        views.setViewVisibility(R.id.scan_progress, View.GONE);
        views.setViewVisibility(R.id.scan_image, View.VISIBLE);
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(mContext);
        mWidget = new ComponentName(mContext, MediaScanWidgetProvider.class);
        appWidgetManager.updateAppWidget(mWidget, views);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;

        final String action = intent.getAction();

        if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
            runScan();
        } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
            stopScan();
        } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {

        }
        super.onReceive(context, intent);

    }

}
